<?php
/**
 * Author: Silent Night
 * email: silentwolf_wp@163.com
 * Date: 2017/10/10
 * Time: 11:02
 */

namespace app\admin\model;

use think\Model;
use think;

class Ruleobj extends Model
{


    public function getID($pid)
    {
        $data = array();
        $res = $this->where(['id' => $pid])
            ->find();
        if ($res) {
            $data = $res->toArray();
        }
        return $data;
    }

    public function lists($w, $pagesize = "10")
    {
        $rdata = array();
        if (!empty($w))
            $this->where($w);
        $this->order("type", "asc");
        $list = $this->paginate($pagesize)->toArray();
//           echo $this->getLastSql();
        return $list;
    }


    public function getDataForWhere($w)
    {
        $data = array();
        $res = $this->where($w)
            ->find();
        if ($res) {
            $data = $res->toArray();
        }
        return $data;
    }

    public function getDataForWheres($w)
    {
        $data = array();
        $res = $this->where($w)
            ->select();
        if ($res) {
            $data = $res->toArray();
        }
        return $data;
    }


    public function getAll1($w = array())
    {
        $data = array();
        if (!empty($w)) {
            $this->where($w);
        }
        $this->order("sorts", "asc");
        $res = $this->select();
        if ($res) {
            $data = $res->toArray();
        }



        return $data;
    }

    public function getAll($w = array())
    {
        $data = array();
        if (!empty($w)) {
            $this->where($w);
        }
        $this->order("sorts", "asc");
        $res = $this->select();
        if ($res) {
            $data = $res->toArray();
        }


        $temp = array();
        if ($data) {
            $second = array();
            $three = array();
            foreach ($data as $key => $val) {

                if ($val['level'] == '1') {
                    $temp[] = $val;
                } else if ($val['level'] == '2') {
                    $second[$val['father']][] = $val;
                } else if ($val['level'] == '3') {
                    $three[$val['father']][] = $val;
                }
            }

            foreach ($temp as $key => $val) {
                $temp[$key]['child'] = array();
                if (isset($second[$val['id']]) && !empty($second[$val['id']])) {
                    $temp[$key]['child'] = $second[$val['id']];
                }
            }

            foreach ($temp as $key => $val) {
                if (!empty($val['child'])) {
                    foreach ($temp[$key]['child'] as $k => $v) {
                        $temp[$key]['child'][$k]['child'] = array();
                        if (isset($three[$v['id']]) && !empty($three[$v['id']])) {
                            $temp[$key]['child'][$k]['child'] = $three[$v['id']];
                        }
                    }
                }
            }

        }

        return $temp;
    }

    public function updateData($update, $id)
    {
        $res = $this->save($update, ['id' => $id]);
        return $res;
    }


    public function updateDataForWhere($update, $where)
    {
        $res = $this->save($update, $where);
        return $res;
    }


    public function add($array)
    {
        $this->save($array);
        return $this->getLastInsID();
    }


    public function delData($w)
    {

        return $this->where($w)->delete();
    }

}